<template>
	<view class="content">
		<view class="search">
			<u--input placeholder="请输入地区" prefixIcon="search" v-model="searchData"
				prefixIconStyle="font-size: 22px;color: #909399"></u--input>
			<u-button @click="show()" type="primary" text="搜索"></u-button>
		</view>
		<view class="result">
			<u-tag text="搜索结果" size="mini" type="warning" style="margin-top: 20px; margin-bottom: 10px;"></u-tag>
			<text style="font-weight: 700; font-size: 28px;">{{result}}</text>
		</view>
		<u-modal :show="isAd" @confirm="confirm" :showCancelButton="true" :content='content'></u-modal>
	</view>

</template>

<script>
	// const quyumadata = require("@/data/quyuma.json")
	export default {
		data() {
			return {
				content: '观看广告后查询地区码',
				isAd: false,
				searchData: '',
				quyuma: [{
						"code": "110000",
						"name": "北京市"
					},
					{
						"code": "120000",
						"name": "天津市"
					},
					{
						"code": "130000",
						"name": "河北省"
					},
					{
						"code": "140000",
						"name": "山西省"
					},
					{
						"code": "150000",
						"name": "内蒙古自治区"
					},
					{
						"code": "210000",
						"name": "辽宁省"
					},
					{
						"code": "220000",
						"name": "吉林省"
					},
					{
						"code": "230000",
						"name": "黑龙江省"
					},
					{
						"code": "310000",
						"name": "上海市"
					},
					{
						"code": "320000",
						"name": "江苏省"
					},
					{
						"code": "330000",
						"name": "浙江省"
					},
					{
						"code": "340000",
						"name": "安徽省"
					},
					{
						"code": "350000",
						"name": "福建省"
					},
					{
						"code": "360000",
						"name": "江西省"
					},
					{
						"code": "370000",
						"name": "山东省"
					},
					{
						"code": "410000",
						"name": "河南省"
					},
					{
						"code": "420000",
						"name": "湖北省"
					},
					{
						"code": "430000",
						"name": "湖南省"
					},
					{
						"code": "440000",
						"name": "广东省"
					},
					{
						"code": "450000",
						"name": "广西壮族自治区"
					},
					{
						"code": "460000",
						"name": "海南省"
					},
					{
						"code": "500000",
						"name": "重庆市"
					},
					{
						"code": "510000",
						"name": "四川省"
					},
					{
						"code": "520000",
						"name": "贵州省"
					},
					{
						"code": "530000",
						"name": "云南省"
					},
					{
						"code": "540000",
						"name": "西藏自治区"
					},
					{
						"code": "610000",
						"name": "陕西省"
					},
					{
						"code": "620000",
						"name": "甘肃省"
					},
					{
						"code": "630000",
						"name": "青海省"
					},
					{
						"code": "640000",
						"name": "宁夏回族自治区"
					},
					{
						"code": "650000",
						"name": "新疆维吾尔自治区"
					},
					{
						"code": "710000",
						"name": "台湾省"
					},
					{
						"code": "810000",
						"name": "香港特别行政区"
					},
					{
						"code": "820000",
						"name": "澳门特别行政区"
					}
				],
				result: '',
				ad: null // 广告
			}
		},
		onLoad() {
			// 创建实例
			this.ad = tt.createRewardedVideoAd({
				adUnitId: "ykjq44zl149w0ldfb3",
			});
			// 监听视频播放完成
			const closeHandler = (data) => {
				tt.hideLoading();
				if (data.isEnded) {
					console.log("观看了", data.count, "个视频");
					this.sousuo()
				} else {
					console.log("未观看完视频");
				}
			};
			this.ad.onClose(closeHandler);
			// do other thing
			// 卸载 close 事件监听
			this.ad.offClose(closeHandler);
			// 预加载资源
			this.ad.load();
		},
		methods: {
			// 观看广告
			confirm() {
				this.ad.show()
				this.isAd = false
				this.sousuo()
			},
			show() {
				this.isAd = true
			},
			sousuo() {
				this.isAd = false
				let isRes = false
				this.quyuma.map((item) => {
					if (item.name == this.searchData) {
						this.result = item.code
						isRes = true
					}
				})
				if (!isRes) {
					this.result = "请输入省份名称"
				}
			}
		}
	}

	// "easycom": {
	// 	"^u-(.*)": "@/node_modules/uview-ui/components/u-$1/u-$1.vue"
	// },
</script>

<style lang="scss">
	.search {
		width: 95%;
		margin: 0 auto;

		.u-button {
			width: 30%;
			margin-top: 10px;
		}
	}

	.result {
		width: 95%;
		margin: 0 auto;
	}
</style>